package com.icoolkj.ms.pay.controller;

import com.icoolkj.ms.common.core.response.ServerResponseEntity;
import com.icoolkj.ms.common.security.AuthUserContext;
import com.icoolkj.ms.api.pay.dto.MemberRechargeDTO;
import com.icoolkj.ms.api.pay.model.MemberRecharge;
import com.icoolkj.ms.pay.service.MemberRechargeService;
import com.icoolkj.ms.pay.service.RechargePackageService;
import com.icoolkj.ms.api.pay.vo.RechargePackageVO;
import com.icoolkj.ms.api.pay.vo.RechargeResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 会员充值控制器
 *
 * @author icoolkj
 */
@Slf4j
@RestController
@RequestMapping("/member")
@RequiredArgsConstructor
@Tag(name = "会员充值接口", description = "会员充值相关接口")
public class MemberRechargeController {

    private final MemberRechargeService memberRechargeService;
    private final RechargePackageService rechargePackageService;

    @GetMapping("/recharge/packages")
    @Operation(summary = "获取充值套餐")
    public ServerResponseEntity<List<RechargePackageVO>> getRechargePackages() {
        return rechargePackageService.getEnabledPackages();
    }

    @PostMapping("/recharge/create")
    @Operation(summary = "创建充值订单")
    public ServerResponseEntity<RechargeResultVO> createRechargeOrder(@Valid @RequestBody MemberRechargeDTO rechargeDTO) {
        Long memberId = AuthUserContext.get().getUserId();
        return memberRechargeService.createRechargeOrder(memberId, rechargeDTO);
    }
    
    @GetMapping("/payment/params")
    @Operation(summary = "获取支付参数")
    public ServerResponseEntity<Map<String, String>> getPaymentParams(
            @RequestParam("orderId") String orderNo,
            @RequestParam("payType") String payType) {
        return memberRechargeService.getPaymentParams(orderNo, payType);
    }

    @GetMapping("/recharge/status/{orderId}")
    @Operation(summary = "查询订单状态")
    public ServerResponseEntity<String> getOrderStatus(@PathVariable("orderId") String orderNo) {
        return memberRechargeService.getOrderStatus(orderNo);
    }

    @GetMapping("/recharge/list")
    @Operation(summary = "查询充值记录")
    public ServerResponseEntity<List<MemberRecharge>> getRechargeRecords() {
        Long memberId = AuthUserContext.get().getUserId();
        return memberRechargeService.getRechargeRecords(memberId);
    }

    @GetMapping("/recharge/detail/{orderNo}")
    @Operation(summary = "查询充值订单详情")
    public ServerResponseEntity<MemberRecharge> getRechargeDetail(@PathVariable String orderNo) {
        return memberRechargeService.getRechargeDetail(orderNo);
    }

    @PostMapping("/recharge/cancel/{orderNo}")
    @Operation(summary = "取消充值订单")
    public ServerResponseEntity<Boolean> cancelRechargeOrder(@PathVariable String orderNo) {
        Long memberId = AuthUserContext.get().getUserId();
        return memberRechargeService.cancelRechargeOrder(memberId, orderNo);
    }
    
    /**
     * 内部回调接口，用于支付成功后业务处理，一般由PayOrderController调用
     * 不直接暴露给外部支付平台调用
     */
    @PostMapping("/internal/recharge/callback/{payType}")
    @Operation(summary = "内部支付结果处理")
    public ServerResponseEntity<Boolean> handlePayCallback(@PathVariable String payType, @RequestBody Map<String, String> params) {
        return memberRechargeService.handlePayNotify(payType, params);
    }
} 